#include "recboxitem.h"
#include "ui_recboxitem.h"

#include <QDebug>
#include <QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    ui->recMusicBtn->hide();

    // 安装事件过滤器
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString &imagePath)
{
    QString style = "border-image: url(" + imagePath + ");";
    ui->recMusicImage->setStyleSheet(style);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->musicImageBox)
    {
        if (QEvent::Enter == event->type())
        {
            // 添加图片上移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(0, 12, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(0, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));

            // 关联信号槽，收到信号时释放对象
            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });

            // 开启动画
            animation->start();
            ui->recMusicBtn->show();
        }
        else if (QEvent::Leave == event->type())
        {
            // 添加图片下移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry");
            animation->setDuration(150);
            animation->setStartValue(QRect(0, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(0, 12, ui->musicImageBox->width(), ui->musicImageBox->height()));

            connect(animation, &QPropertyAnimation::finished, this, [=](){
                delete animation;
            });

            animation->start();
            ui->recMusicBtn->hide();
        }
        return true;
    }
    return QWidget::eventFilter(watched, event);
}
